package gwt.bbsbot.client.widget;

import gwt.bbsbot.client.DetailService;
import gwt.bbsbot.client.DetailServiceAsync;
import gwt.bbsbot.client.beans.QuestionDataBean;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.VerticalPanel;


public class DetailBbsWidget extends BbsWidget {

	protected final DetailServiceAsync detailService = GWT
	.create(DetailService.class);

	private String id;
	private Panel panelFxQuestion;
	private FlexTable fxQuestion;
//	private Panel panelFxQuestion = new VerticalPanel();
//	private FlexTable fxQuestion = new FlexTable();
	
	public String getId() {
		return id;
	}

	public DetailBbsWidget(String id) {
		this.id = id;
		
		getQuestion();
	}

	protected void init(){
		this.panelBbs.add(lbTitle);
		panelFxQuestion = new VerticalPanel();
		fxQuestion = new FlexTable();
		this.fxQuestion.toString();
		this.panelFxQuestion.toString();

		this.panelFxQuestion.add(this.fxQuestion);
		this.panelFxBbs.add(this.fxTable);
		this.panelBbs.add(this.panelFxQuestion);
		this.panelBbs.add(this.panelFxBbs);
		this.panelBbs.add(this.btNew);
		
		this.btNew.addClickHandler(new ClickHandler(){

			@Override
			public void onClick(ClickEvent event) {
				dlgInputForm.center();
			}
		});
		createInputForm();
		this.add(this.panelBbs);
	}

	
	@Override
	public void displayBBS() {

	}
	
	private void getQuestion(){
		this.detailService.getQuestion(Long.parseLong(id), new AsyncCallback<QuestionDataBean>(){

			@Override
			public void onFailure(Throwable caught) {
				debug.setError(caught.getMessage());
			}

			@Override
			public void onSuccess(QuestionDataBean bean) {
				FlexTable tmpTable = new FlexTable();
				tmpTable.setBorderWidth(1);
				tmpTable.setText(0, 0, "ID");
				tmpTable.setText(0, 1, "投稿者");
				tmpTable.setText(0, 2, "内容");
				
				//質問詳細リンク作成
				tmpTable.setText(1, 0, bean.getId().toString());
				
				//質問者のアイコンとか設定
				HTML name = new HTML();
				if(bean.getProfileImageURL() == null){
					name.setHTML(bean.getName());
				}else{
					name.setHTML("<img src=\"" + bean.getProfileImageURL() + "\" /><br>" + bean.getName());
				}
				tmpTable.setWidget(1, 1, name);
				tmpTable.setText(1, 2, bean.getQuestionData());
				
				if(panelFxQuestion != null){
					panelFxQuestion.remove(fxQuestion);
				}
				fxQuestion = tmpTable;
				panelFxQuestion.add(fxQuestion);

				System.out.println(bean.toString());
				
			}
			
		});
	}

	@Override
	protected void createAddHandler(Button button, String name, String context) {
		// TODO Auto-generated method stub
		
	}

}
